import axios from "axios";
// import { getToken } from "@/utils/auth";
// import Message from "@/components/message";
// const token = 'eyJhbGciOiJIUzUxMiJ9.eyJsb2dpbl91c2VyX2tleSI6ImUxNDdkNGRiLTQ1MzgtNDQxNy1iY2YzLTljOGM4MzdmMTRkZSJ9.wFHx33-UlYPHmih1aAPt0ZdZrP6UG6A2lWM65DyGdJeP8Av_84caV3PTfjocyfebP0sV5PRhOA0a2z_ZWiBpzQ'
// const token='eyJhbGciOiJIUzUxMiJ9.eyJsb2dpbl91c2VyX2tleSI6ImNkZjlmMDY2LTBmMmEtNGJmMC04YTc1LWQ3ZWMxYzNiNzkwMCJ9.wO_1TrQEYtRgoDUrlhASnXifDg0vXUhHcQ73H10tEa7GXae02igOIdDrv0S8L5aVbW5QbzsBZkvRAp19fAsf9Q'
// const token = sessionStorage.getItem('token') || '';
//跨域认证信息 header 名
const xsrfHeaderName = "Authorization";

axios.defaults.withCredentials = false;
axios.defaults.xsrfHeaderName = xsrfHeaderName;
axios.defaults.headers["Content-Type"] = "application/json;charset=utf-8";


// 创建axios实例
const service = axios.create({
  baseURL: import.meta.env.VITE_BASE_URL,
  // 超时
  timeout: 10000
});

// request拦截器
service.interceptors.request.use(
  config => {
    // console.log("config------", config)
    // 是否需要设置 token
    const isToken = (config.headers || {}).isToken === false;
    const token = sessionStorage.getItem('token') || '';
    if (token && !isToken) {
      config.headers["Authorization"] = token; // 让每个请求携带自定义token 请根据实际情况自行修改
    }
    return config;
  },
  error => {
    console.log(error);
    Promise.reject(error);
  }
);

// 响应拦截器
service.interceptors.response.use(
  res => {
    // 未设置状态码则默认成功状态
    const code = res.data.code || 200;
    // 二进制数据则直接返回
    if (
      res.request.responseType === "blob" ||
      res.request.responseType === "arraybuffer"
    ) {
      return res;
    }
    if (code === 401) {
      // Message.install({
      //   content: "认证失败，无法访问系统资源",
      //   callBack: () => {
      //     location.href = "/index";
      //   }
      // });
    } else {
      return res;
    }
  },
  error => {
    console.log("err" + error);
    return Promise.reject(error);
  }
);

export default service